﻿Imports System.Data.SqlClient

Public Class AdmFindMember
    Inherits System.Web.UI.Page
    Dim strErr As String = ""
    Private i As Integer
    Private pages As Integer
    Dim strSql As String = ""
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Session("admin_ID") <> Nothing And Session("admin_ID") <> "") Then
            If Request("active") <> Nothing And Request("active") <> "" Then

                Dim strHtml As String = ""
                Dim strEmail, strPhone As String
                Dim strMid, email, phone, cellphone, active, activeT As String
                Dim HTMLString As String = ""
                Dim sSQL As String = ""
                Dim objfind As New GetData
                Dim objrd As SqlDataReader = Nothing
                Dim ps As New PreparedStatement(sSQL)
                Dim pss As New PreparedStatement(sSQL)
                Dim act, actt As String
                Dim sum, sumdesc As Integer
                i = Request("page")
                If i = Nothing Or i = 0 Then
                    i = 1
                End If
                strEmail = Request("email")
                strPhone = Request("phone")
                strMid = Request("mid")
                active = Request("active")
                If active = 1 Then
                    activeT = 0
                Else
                    activeT = 1
                End If
                sSQL = "update member set active=@activeT where GAPortal_ID=@mid"
                ps.addParameter("mid", strMid)
                ps.addParameter("activeT", activeT)
                ps.strSql = sSQL
                objfind.es(ps, strErr)


                searchResult.InnerHtml = ""
                strSql = ""
                If strEmail <> "" Then
                    strSql = " and Upper(email) like @email"
                End If
                If strPhone <> "" Then
                    strSql = " and (Upper(phone) like @phone or Upper(cellphone) like @phone)"
                End If
                sSQL = "select count(*) from member where 1=1"
                sSQL += strSql
                pss.strSql = sSQL
                pss.parameters.Clear()
                pss.addParameter("email", UCase(strEmail))
                pss.addParameter("phone", UCase(strPhone))
                Dim conn As SqlConnection = objfind.getConn()

                objrd = objfind.dr(pss, strErr, conn, objrd)
                objrd.Read()
                sum = objrd.Item(0)
                pages = (sum \ 10) + 1
                If i >= pages Then
                    i = pages
                ElseIf i <= 0 Then
                    i = 1
                End If
                If (i = pages) Then
                    sumdesc = sum
                Else
                    sumdesc = i * 10
                End If


                sSQL = "select top(" & i & "*10) * from member where 1=1"
                sSQL += strSql
                sSQL += " and GAPortal_ID not in (select top (" & i - 1 & "*10) GAPortal_ID from member where 1=1"
                sSQL += strSql
                sSQL += " order by GAPortal_ID) and GAPortal_ID not in (select top(" & sum - sumdesc & ") GAPortal_ID from member where 1=1"
                sSQL += strSql
                sSQL += " order by GAPortal_ID desc) order by GAPortal_ID"
                pss.strSql = sSQL
                objrd = objfind.dr(pss, strErr, conn, objrd)


                If objrd.HasRows Then
                    strHtml += "<table class='minorbox' cellspacing='1' width='100%' border=""0""  style=""background-color:#3399FF"">"


                    strHtml += "<td class='gridheader' style=""background-color:#AACCFF"">Email</td>"

                    strHtml += "<td class='gridheader' style=""background-color:#AACCFF"">Phone</td>"

                    strHtml += "<td class='gridheader' style=""background-color:#AACCFF"">Cellphone</td>"
                    strHtml += "<td class='gridheader' style=""background-color:#AACCFF"">Active</td>"
                    strHtml += "<td class='gridheader' style=""background-color:#AACCFF"">Edit</td>"
                    strHtml += "<td class='gridheader' style=""background-color:#AACCFF"">Membership center</td></tr>"

                    While (objrd.Read())
                        strMid = objrd("GAPortal_ID").ToString
                        email = objrd("email").ToString
                        phone = objrd("phone").ToString
                        cellphone = objrd("cellphone").ToString
                        active = objrd("active").ToString
                        If active = 1 Then
                            act = "无效"
                            actt = "true"
                        Else
                            act = "有效"
                            actt = "false"
                        End If
                        HTMLString = HTMLString & vbCr & vbTab & vbTab & "<tr>"

                        HTMLString = HTMLString & "<td valign=""top"" style=""background-color:#FFF"">" & email & "</td>"
                        HTMLString = HTMLString & "<td valign=""top"" style=""background-color:#FFF"">" & phone & "</td>"
                        HTMLString = HTMLString & "<td valign=""top"" style=""background-color:#FFF"">" & cellphone & "</td>"
                        HTMLString = HTMLString & "<td valign=""top"" style=""background-color:#FFF"">" & actt & "</td>"
                        HTMLString = HTMLString & "<td valign=""top"" style=""background-color:#FFF""><a href=""admfindmember.aspx?mid=" & strMid & "&active=" & active & "&email=" & strEmail & "&phone=" & strPhone & "&page=" & i & """>" & act & "&nbsp;</a>"
                        HTMLString = HTMLString & "<a href=""admeditmember.aspx?mid=" & strMid & "&langFlag=en ""> English</a>&nbsp;"
                        HTMLString = HTMLString & "<a href=""admeditmember.aspx?mid=" & strMid & "&langFlag=zh "" > Chinese</a></td>"
                        HTMLString = HTMLString & "<td valign=""top"" style=""background-color:#FFF""><a href=""adm_merchantbaseinfo.aspx?uid=" & strMid & """> English</a>&nbsp;"
                        HTMLString = HTMLString & "<a href=""adm_cn_merchantbaseinfo.aspx?uid=" & strMid & """ > Chinese</a></td></tr>"
                    End While

                    HTMLString = HTMLString & "</table>"
                    strHtml += HTMLString
                    pageNav.Visible = True
                    pageNav.InnerHtml = "<a href=""admfindmember.aspx?page=1" & "&email=" & strEmail & "&phone=" & strPhone & """>home &nbsp;</a>" & _
                                        "<a href=""admfindmember.aspx?page=" & i - 1 & "&email=" & strEmail & "&phone=" & strPhone & """>previous page &nbsp;</a>" & _
                                        "<a href=""admfindmember.aspx?page=" & i + 1 & "&email=" & strEmail & "&phone=" & strPhone & """>next page &nbsp;</a>" & _
                                        "<a href=""admfindmember.aspx?page=" & pages & "&email=" & strEmail & "&phone=" & strPhone & """>end &nbsp;</a>"
                    pg.Visible = True
                    totalPage.InnerText = i.ToString & "/" & pages.ToString()

                    searchResult.InnerHtml = strHtml
                End If
                objfind.closeConn(conn)


            ElseIf Request("page") <> "" Or Request("page") <> "" Then

                Dim strHtml As String = ""
                Dim strEmail, strPhone As String
                Dim strMid, email, phone, cellphone, active As String
                Dim HTMLString As String = ""
                Dim sSQL As String = ""
                Dim objfind As New GetData
                Dim objrd As SqlDataReader = Nothing
                Dim ps As New PreparedStatement(sSQL)
                Dim act, actt As String
                Dim sum, sumdesc As Integer

                i = Request("page")
                If i = Nothing Or i = 0 Then
                    i = 1
                End If
                strEmail = Request("email")
                strPhone = Request("phone")
                strSql = ""
                If strEmail <> "" Then
                    strSql = " and Upper(email) like @email"
                End If
                If strPhone <> "" Then
                    strSql = " and (Upper(phone) like @phone or Upper(cellphone) like @phone)"
                End If

                sSQL = "select count(*) from member where 1=1"
                sSQL += strSql
                ps.strSql = sSQL
                ps.parameters.Clear()
                ps.addParameter("email", UCase(strEmail))
                ps.addParameter("phone", UCase(strPhone))
                Dim conn As SqlConnection = objfind.getConn()

                objrd = objfind.dr(ps, strErr, conn, objrd)
                objrd.Read()
                sum = objrd.Item(0)
                pages = (sum \ 10) + 1
                If i >= pages Then
                    i = pages
                ElseIf i <= 0 Then
                    i = 1
                End If
                If (i = pages) Then
                    sumdesc = sum
                Else
                    sumdesc = i * 10
                End If


                sSQL = "select top(" & i & "*10) * from member where 1=1"
                sSQL += strSql
                sSQL += " and GAPortal_ID not in (select top (" & i - 1 & "*10) GAPortal_ID from member where 1=1"
                sSQL += strSql
                sSQL += " order by GAPortal_ID) and GAPortal_ID not in (select top(" & sum - sumdesc & ") GAPortal_ID from member where 1=1"
                sSQL += strSql
                sSQL += " order by GAPortal_ID desc) order by GAPortal_ID"
                ps.strSql = sSQL
                objrd = objfind.dr(ps, strErr, conn, objrd)

                If objrd.HasRows Then
                    strHtml += "<table class='minorbox' cellspacing='1' width='100%' border=""0""  style=""background-color:#3399FF"">"


                    strHtml += "<td class='gridheader' style=""background-color:#AACCFF"">Email</td>"

                    strHtml += "<td class='gridheader' style=""background-color:#AACCFF"">Phone</td>"

                    strHtml += "<td class='gridheader' style=""background-color:#AACCFF"">Cellphone</td>"

                    strHtml += "<td class='gridheader' style=""background-color:#AACCFF"">Active</td>"
                    strHtml += "<td class='gridheader' style=""background-color:#AACCFF"">Edit</td>"
                    strHtml += "<td class='gridheader' style=""background-color:#AACCFF"">Membership center</td></tr>"

                    While (objrd.Read())
                        strMid = objrd("GAPortal_ID").ToString
                        email = objrd("email").ToString
                        phone = objrd("phone").ToString
                        cellphone = objrd("cellphone").ToString
                        active = objrd("active").ToString
                        If active = 1 Then
                            act = "无效"
                            actt = "true"
                        Else
                            act = "有效"
                            actt = "false"
                        End If
                        HTMLString = HTMLString & vbCr & vbTab & vbTab & "<tr>"

                        HTMLString = HTMLString & "<td valign=""top"" style=""background-color:#FFF"">" & email & "</td>"
                        HTMLString = HTMLString & "<td valign=""top"" style=""background-color:#FFF"">" & phone & "</td>"
                        HTMLString = HTMLString & "<td valign=""top"" style=""background-color:#FFF"">" & cellphone & "</td>"
                        HTMLString = HTMLString & "<td valign=""top"" style=""background-color:#FFF"">" & actt & "</td>"
                        HTMLString = HTMLString & "<td valign=""top"" style=""background-color:#FFF""><a href=""admfindmember.aspx?mid=" & strMid & "&active=" & active & "&email=" & strEmail & "&phone=" & strPhone & "&page=" & i & """>" & act & "&nbsp;</a>"
                        HTMLString = HTMLString & "<a href=""admeditmember.aspx?mid=" & strMid & "&langFlag=en ""> English</a>&nbsp;"
                        HTMLString = HTMLString & "<a href=""admeditmember.aspx?mid=" & strMid & "&langFlag=zh "" > Chinese</a></td>"
                        HTMLString = HTMLString & "<td valign=""top"" style=""background-color:#FFF""><a href=""adm_merchantbaseinfo.aspx?uid=" & strMid & """> English</a>&nbsp;"
                        HTMLString = HTMLString & "<a href=""adm_cn_merchantbaseinfo.aspx?uid=" & strMid & """> Chinese</a></td></tr>"
                    End While

                    HTMLString = HTMLString & "</table>"
                    strHtml += HTMLString
                    pageNav.Visible = True
                    pageNav.InnerHtml = "<a href=""admfindmember.aspx?page=1" & "&email=" & strEmail & "&phone=" & strPhone & """>home &nbsp;</a>" & _
                                        "<a href=""admfindmember.aspx?page=" & i - 1 & "&email=" & strEmail & "&phone=" & strPhone & """>previous page &nbsp;</a>" & _
                                        "<a href=""admfindmember.aspx?page=" & i + 1 & "&email=" & strEmail & "&phone=" & strPhone & """>next page &nbsp;</a>" & _
                                        "<a href=""admfindmember.aspx?page=" & pages & "&email=" & strEmail & "&phone=" & strPhone & """>end &nbsp;</a>"
                    pg.Visible = True
                    totalPage.InnerText = i.ToString & "/" & pages.ToString()


                End If
                objfind.closeConn(conn)

                searchResult.InnerHtml = strHtml
            End If
        Else
            Response.Redirect("../adm_login.aspx")
        End If
    End Sub

    Protected Sub btnFind_Click(sender As Object, e As EventArgs) Handles btnFind.Click
        Dim strHtml As String = ""
        Dim strEmail, strPhone As String
        Dim strMid, email, phone, cellphone, active As String
        Dim HTMLString As String = ""
        Dim sSQL As String = ""
        Dim objfind As New GetData
        Dim objrd As SqlDataReader = Nothing
        Dim ps As New PreparedStatement(sSQL)
        Dim act, actt As String
        Dim sum, sumdesc As Integer
        i = Request("page")
        If i = Nothing Or i = 0 Then
            i = 1
        End If
        strEmail = txtEmail.Text
        strPhone = txtPhone.Text
        strSql = ""
        If strEmail <> "" Then
            strSql = " and Upper(email) like @email"
        End If
        If strPhone <> "" Then
            strSql = " and (Upper(phone) like @phone or Upper(cellphone) like @phone)"
        End If

        sSQL = "select count(*) from member where 1=1"
        sSQL += strSql
        ps.strSql = sSQL
        ps.parameters.Clear()
        ps.addParameter("email", UCase(strEmail))
        ps.addParameter("phone", UCase(strPhone))
        Dim conn As SqlConnection = objfind.getConn()

        objrd = objfind.dr(ps, strErr, conn, objrd)
        objrd.Read()
        sum = objrd.Item(0)
        pages = (sum \ 10) + 1
        If i >= pages Then
            i = pages
        ElseIf i <= 0 Then
            i = 1
        End If
        If (i = pages) Then
            sumdesc = sum
        Else
            sumdesc = i * 10
        End If


        sSQL = "select top(" & i & "*10) * from member where 1=1"
        sSQL += strSql
        sSQL += " and GAPortal_ID not in (select top (" & i - 1 & "*10) GAPortal_ID from member where 1=1"
        sSQL += strSql
        sSQL += " order by GAPortal_ID) and GAPortal_ID not in (select top(" & sum - sumdesc & ") GAPortal_ID from member where 1=1"
        sSQL += strSql
        sSQL += " order by GAPortal_ID desc) order by GAPortal_ID"
        ps.strSql = sSQL
        objrd = objfind.dr(ps, strErr, conn, objrd)

        If objrd.HasRows Then
            strHtml += "<table class='minorbox' cellspacing='1' width='100%' border=""0""  style=""background-color:#3399FF"">"


            strHtml += "<td class='gridheader' style=""background-color:#AACCFF"">Email</td>"

            strHtml += "<td class='gridheader' style=""background-color:#AACCFF"">Phone</td>"

            strHtml += "<td class='gridheader' style=""background-color:#AACCFF"">Cellphone</td>"

            strHtml += "<td class='gridheader' style=""background-color:#AACCFF"">Active</td>"
            strHtml += "<td class='gridheader' style=""background-color:#AACCFF"">Edit</td>"
            strHtml += "<td class='gridheader' style=""background-color:#AACCFF"">Membership center</td></tr>"

            While (objrd.Read())
                strMid = objrd("GAPortal_ID").ToString
                email = objrd("email").ToString
                phone = objrd("phone").ToString
                cellphone = objrd("cellphone").ToString
                active = objrd("active").ToString
                If active = 1 Then
                    act = "无效"
                    actt = "true"
                Else
                    act = "有效"
                    actt = "false"
                End If
                HTMLString = HTMLString & vbCr & vbTab & vbTab & "<tr>"

                HTMLString = HTMLString & "<td valign=""top"" style=""background-color:#FFF"">" & email & "</td>"
                HTMLString = HTMLString & "<td valign=""top"" style=""background-color:#FFF"">" & phone & "</td>"
                HTMLString = HTMLString & "<td valign=""top"" style=""background-color:#FFF"">" & cellphone & "</td>"
                HTMLString = HTMLString & "<td valign=""top"" style=""background-color:#FFF"">" & actt & "</td>"
                HTMLString = HTMLString & "<td valign=""top"" style=""background-color:#FFF""><a href=""admfindmember.aspx?mid=" & strMid & "&active=" & active & "&email=" & strEmail & "&phone=" & strPhone & "&page=" & i & """>" & act & "&nbsp;</a>"
                HTMLString = HTMLString & "<a href=""admeditmember.aspx?mid=" & strMid & "&langFlag=en ""> English</a>&nbsp;"
                HTMLString = HTMLString & "<a href=""admeditmember.aspx?mid=" & strMid & "&langFlag=zh "" > Chinese</a></td>"
                HTMLString = HTMLString & "<td valign=""top"" style=""background-color:#FFF""><a href=""adm_merchantbaseinfo.aspx?uid=" & strMid & """> English</a>&nbsp;"
                HTMLString = HTMLString & "<a href=""adm_cn_merchantbaseinfo.aspx?uid=" & strMid & """> Chinese</a></td></tr>"
            End While

            HTMLString = HTMLString & "</table>"
            strHtml += HTMLString
            pageNav.Visible = True
            pageNav.InnerHtml = "<a href=""admfindmember.aspx?page=1" & "&email=" & strEmail & "&phone=" & strPhone & """>home &nbsp;</a>" & _
                                "<a href=""admfindmember.aspx?page=" & i - 1 & "&email=" & strEmail & "&phone=" & strPhone & """>previous page &nbsp;</a>" & _
                                "<a href=""admfindmember.aspx?page=" & i + 1 & "&email=" & strEmail & "&phone=" & strPhone & """>next page &nbsp;</a>" & _
                                "<a href=""admfindmember.aspx?page=" & pages & "&email=" & strEmail & "&phone=" & strPhone & """>end &nbsp;</a>"
            pg.Visible = True
            totalPage.InnerText = i.ToString & "/" & pages.ToString()
        Else
            strHtml = "No member was found"

        End If
        objfind.closeConn(conn)

        searchResult.InnerHtml = strHtml
    End Sub
End Class